<?php
/**
 * 商品分类管理
 *
 * @copyright  Copyright (c) 2015-2025 NcPHP Inc. (http://www.ncphp.com)
 * @license    http://www.ncphp.com/
 * @link       http://www.ncphp.com/
 * @author	   NcPHP Team
 * @since      File available since Release v2.1
 */
defined('InNcPHP') or exit('Access Invalid!');

class ticket_categoryControl extends SystemControl{
	private $links = array(
		array('url'=>'act=ticket_category&op=index','lang'=>'nc_manage'),
		array('url'=>'act=ticket_category&op=add','lang'=>'nc_new'),
	);
	public function __construct(){
		parent::__construct();
		Language::read('ticket_category');
	}

	/**
	 * 分类管理
	 */
	public function indexOp(){
        $lang	= Language::getLangContent();
		/* @var ticket_categoryModel $model_class */
		$model_class = Model('ticket_category');

		//列表
		$cate_list = $model_class->getCategoryTree();
		foreach ($cate_list as $k => $v) {
            $cate_list[$k]['tc_icon'] = UPLOAD . 'product/' . $cate_list[$k]['tc_icon'];
            foreach ($cate_list[$k]['child'] as $kk => $vv) {
                $cate_list[$k]['child'][$kk]['tc_icon'] = UPLOAD . 'product/' . $cate_list[$k]['child'][$kk]['tc_icon'];
            }
		}

        Tpl::output('class_list',$cate_list);
        Tpl::output('top_link',$this->sublink($this->links,'index'));
        Tpl::showpage('ticket_category.index');
	}

	/**
	 * 商品分类添加
	 */
	public function addOp(){
		$lang	= Language::getLangContent();
        /* @var ticket_categoryModel $model_class */
		$model_class = Model('ticket_category');
		if (chksubmit()){
			$obj_validate = new Validate();
			$obj_validate->validateparam = array(
				array("input"=>$_POST["tc_name"], "require"=>"true", "message"=>$lang['ticket_cate_add_name_null']),
				array("input"=>$_POST["tc_sort"], "require"=>"true", 'validator'=>'Number', "message"=>$lang['ticket_cate_add_sort_int']),
			);
			$error = $obj_validate->validate();
			if ($error != ''){
				showMessage($error);
			}else {
				$insert_array = array();
				$insert_array['tc_name']		= $_POST['tc_name'];
                $insert_array['tc_subname']		= $_POST['tc_subname'];
				$insert_array['tc_pid']	= intval($_POST['tc_pid']);
				$insert_array['tc_sort']		= intval($_POST['tc_sort']);
                //上传图片
                if (!empty($_FILES['pic']['name'])) {
                    $upload = new UploadFile();
                    $upload->set('default_dir','product/' . $upload->getSysSetPath());
                    $upload->upfile('pic');
                    $insert_array['tc_icon'] = $upload->getSysSetPath() . $upload->file_name;
                }
				$result = $model_class->addCategory($insert_array);
				if ($result){
					$url = array(
						array(
							'url'=>'index.php?act=ticket_category&op=add&pid='.$_POST['tc_pid'],
							'msg'=>$lang['ticket_cate_add_again'],
						),
						array(
							'url'=>'index.php?act=ticket_category&op=index',
							'msg'=>$lang['ticket_cate_add_back_to_list'],
						)
					);
					$this->log(L('nc_add,ticket_cate_index_class').'['.$_POST['tc_name'].']',1);
					showMessage($lang['nc_common_save_succ'],$url);
				}else {
					$this->log(L('nc_add,ticket_cate_index_class').'['.$_POST['tc_name'].']',0);
					showMessage($lang['nc_common_save_fail']);
				}
			}
		}

        //父ID
        $pid = $_GET['pid'] ? intval($_GET['pid']) : 0;
        Tpl::output('pid', $pid);
		//父类列表，只取到第二级
		$parent_list = $model_class->getCategoryList(array('tc_pid'=>0));
		Tpl::output('parent_list', $parent_list);
		Tpl::output('top_link',$this->sublink($this->links,'add'));
		Tpl::showpage('ticket_category.add');
	}

    /**
     * 字段校验
     */
	public function checkOp()
    {
        switch ($_GET['type']) {
            case 'tc_name':
                /* @var ticket_categoryModel $model_class */
                $model_class = Model('ticket_category');
                $condition = array(
                    'tc_name' => $_GET['tc_name'],
                    'tc_pid' => $_GET['tc_pid']
                );
                $info = $model_class->getCategoryInfo($condition);
                exit($info ? 'false' : 'true');
            case 'tc_name_update':
                /* @var ticket_categoryModel $model_class */
                $model_class = Model('ticket_category');
                $condition = array();
                $condition['tc_id'] = array('neq', $_GET['tc_id']);
                $condition['tc_name'] = $_GET['tc_name'];
                $condition['tc_pid'] = $_GET['tc_pid'];
                $info = $model_class->getCategoryInfo($condition);
                exit($info ? 'false' : 'true');
            default:
                exit('false');
        }
    }
	
	/**
	 * 编辑
	 */
	public function editOp(){
		$lang	= Language::getLangContent();
        /* @var ticket_categoryModel $model_class */
        $model_class = Model('ticket_category');
		
		if (chksubmit()){
			$obj_validate = new Validate();
			$obj_validate->validateparam = array(
				array("input"=>$_POST["tc_name"], "require"=>"true", "message"=>$lang['ticket_cate_add_name_null']),
				array("input"=>$_POST["tc_sort"], "require"=>"true", 'validator'=>'Number', "message"=>$lang['ticket_cate_add_sort_int']),
			);
			$error = $obj_validate->validate();
			if ($error != ''){
				showMessage($error);
			}

			// 更新分类信息
            $tc_id = intval($_POST['tc_id']);
			$update_array = array();
			$update_array['tc_name'] 		= $_POST['tc_name'];
            $update_array['tc_subname'] 		= $_POST['tc_subname'];
			$update_array['tc_sort']		= intval($_POST['tc_sort']);
            //上传图片
            if (!empty($_FILES['pic']['name'])) {
                $upload = new UploadFile();
                $upload->set('default_dir','product/' . $upload->getSysSetPath());
                $upload->upfile('pic');
                $update_array['tc_icon'] = $upload->getSysSetPath() . $upload->file_name;
            }
			$result = $model_class->updateCategory(array('tc_id'=>$tc_id), $update_array);
			if (!$result){
				$this->log(L('nc_edit,ticket_cate_index_class').'['.$_POST['tc_name'].']',0);
				showMessage($lang['ticket_cate_batch_edit_fail']);
			}

			$url = array(
				array(
					'url'=>'index.php?act=ticket_category&op=edit&id='.intval($_POST['tc_id']),
					'msg'=>$lang['ticket_cate_batch_edit_again'],
				),
				array(
					'url'=>'index.php?act=ticket_category&op=index',
					'msg'=>$lang['ticket_cate_add_back_to_list'],
				)
			);
			$this->log(L('nc_edit,ticket_cate_index_class').'['.$_POST['tc_name'].']',1);
			showMessage($lang['ticket_cate_batch_edit_ok'],$url,'html','succ',1,5000);
		}

        $cate = $model_class->getCategoryInfo(array('tc_id'=>$_GET['id']));
		$cate['tc_icon'] = UPLOAD . 'product/' . $cate['tc_icon'];
		Tpl::output('cate',$cate);
		Tpl::output('top_link',$this->sublink($this->links,'index'));
		Tpl::showpage('ticket_category.edit');
	}

	/**
	 * 删除分类
	 */
	public function delOp(){
		$lang	= Language::getLangContent();
        /* @var ticket_categoryModel $model_class */
        $model_class = Model('ticket_category');
        $tc_id = intval($_GET['id']);
		if ($tc_id > 0){
			//删除分类
			$model_class->delCategory($tc_id);
			$this->log(L('nc_delete,ticket_cate_index_class') . '[tc_id:' . $tc_id . ']',1);
			showMessage($lang['nc_common_del_succ'],'index.php?act=ticket_category&op=index');
		}else {
			$this->log(L('nc_delete,ticket_cate_index_class') . '[tc_id:' . $tc_id . ']',0);
			showMessage($lang['nc_common_del_fail'],'index.php?act=ticket_category&op=index');
		}
	}
}